-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeighted Graph implementation using Adjacency Matrix.cpp
54 lines (50 loc) · 1.32 KB
/
Weighted Graph implementation using Adjacency Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
using namespace std;
class Graph {
private:
int m = 0;
bool directed;
vector<vector<int>> matrix;
int w = 0;
public:
Graph(int nodes, bool dir = false){ // Constructor
m = nodes;
directed = dir;
matrix = vector<vector<int>>(m, vector<int>(m, INT_MAX));
}
void addEdge(int u, int v){
if(u >= m || v >= m){
cout << "Invalid edge [" << u << "]🠨🠪[" << v << "]" << endl << "Node value should be max (m-1)" << endl << endl;
} else {
matrix[u][v] = ++w;
if(!directed) matrix[v][u] = w;
}
}
void print(){
for (int row = 0; row < matrix.size(); row++){
for (int col = 0; col < matrix[row].size(); col++){
if(matrix[row][col] == INT_MAX){
cout << " ∞";
} else {
cout << (to_string(matrix[row][col]).size() > 1 ? " " : " ") << matrix[row][col];
}
}
cout << endl;
}
}
};
int main(){
int m = 6;
Graph G(m);
G.addEdge(0, 2);
G.addEdge(0, 4);
G.addEdge(1, 0);
G.addEdge(2, 3);
G.addEdge(2, 5);
G.addEdge(3, 4);
G.addEdge(4, 5);
G.addEdge(4, 1);
cout << "Adjacency Matrix: " << endl;
G.print();
}